package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.dao.BedPutDao;
import com.bed.lovewithpatientbed.dao.DeptDao;
import com.bed.lovewithpatientbed.dao.DeptDeptmanagerDao;
import com.bed.lovewithpatientbed.dao.DeptManagerDao;
import com.bed.lovewithpatientbed.pojo.*;
import com.bed.lovewithpatientbed.service.DeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yg
 * @version 1.0.0
 * @ClassName HospitalService.java
 * @Description TODO
 */

@Service
@Transactional
public class DeptServiceImpl implements DeptService {

    @Resource
    private DeptDao deptDao;

    @Resource
    private DeptManagerDao deptManagerDao;

    @Resource
    private BedPutDao bedPutDao;

    @Resource
    private DeptDeptmanagerDao deptDeptmanagerDao;

    //全查
    @Override
    public List<Dept> getAll() {
        return deptDao.selectByExample(null);
    }


    //根据id查找
    @Override
    public Dept selectById(Integer did) {
        return deptDao.selectByPrimaryKey(did);
    }

    //新增科室信息
    @Override
    public int addDept(Dept dept) {

        return deptDao.insertSelective(dept);
    }

    //根据id删除科室信息
    @Override
    public int delDept(Dept dept) {
        //软删除
        dept.setSoftdel(0);
        return deptDao.insert(dept);
    }

    //根据id批量删除
    @Override
    public int delbatchDept(Dept[] depts) {
        int countRow=0;
        if(depts.length!=0){
            for (Dept dept:depts
                 ) {
                dept.setSoftdel(0);
                deptDao.insert(dept);
                countRow+=1;
            }
        }
        return countRow;
    }

    @Override
    public int updateDept(Dept dept) {
        return deptDao.updateByPrimaryKeySelective(dept);
    }

    @Override
    public long countRow() {
        return deptDao.countByExample(null);
    }

    /**
         * @title 根据医院id和科室id查询床位投放数量
         * @description
         * @author yg
         * @updateTime
         * @throws
         */

    @Override
    public long countbed(BedPut bedPut) {
        BedPutExample bedPutExample = new BedPutExample();
        BedPutExample.Criteria criteria = bedPutExample.createCriteria();

        if (bedPut.getHospitalId()!=null&&!"".equals(bedPut.getHospitalId())){
            if (bedPut.getDeptId()!=null&&!"".equals(bedPut.getDeptId())){
                criteria.andHospitalIdEqualTo(bedPut.getHospitalId());
                criteria.andDeptIdEqualTo(bedPut.getDeptId());
            }
        }
        long l = bedPutDao.countByExample(bedPutExample);
        return l;
    }

    /**
     * @title getDeptIds
     * @description 根据科室名称获取科室id
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public List<Integer> getDeptIds(String deptName) {
        DeptExample deptExample = new DeptExample();
        DeptExample.Criteria criteria = deptExample.createCriteria();
        criteria.andDeptNameEqualTo(deptName);
        List<Dept> deptList = deptDao.selectByExample(deptExample);
        List<Integer> deptIds = new ArrayList<>();
        for (Dept dept:deptList) {
            Integer deptId = dept.getDeptId();
            deptIds.add(deptId);
        }
        return deptIds;
    }

    /**
         * @title 查找科室负责人
         * @description
         * @author yg
         * @updateTime
         * @throws
         */

    @Override
    public DeptManager selectByDeptIdHospitalId(Integer deptid, Integer hospitalid) {

        List<DeptDeptmanager> deptmanagerList=null;
        if (deptid!=null&&!"".equals(deptid)){
            if (hospitalid!=null&&!"".equals(hospitalid)){
                DeptDeptmanagerExample deptDeptmanagerExample=new DeptDeptmanagerExample();
                DeptDeptmanagerExample.Criteria criteria = deptDeptmanagerExample.createCriteria();
                criteria.andDeptIdEqualTo(deptid);
                criteria.andHospitalIdEqualTo(hospitalid);
                deptmanagerList = deptDeptmanagerDao.selectByExample(deptDeptmanagerExample);
            }
        }
        Integer deptmanagerid= deptmanagerList.get(0).getDeptManagerId();
        DeptManager deptManager = deptManagerDao.selectByPrimaryKey(deptmanagerid);

        return deptManager;
    }
}
